﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SdmxMl.UC
{
    /// <summary>
    /// Date control with nullable date and copy paste.
    /// </summary>
    public partial class UcDatePicker : UserControl
    {
        private static string dtFormat = "yyyy-MM-dd";

        /// <summary> Set finalized value (no changes allowed) </summary>
        public bool IsFinal
        {
            set 
            {
                bool isFinal = value;
                dateTimePick.Enabled = ! isFinal;
                buttonClear.Enabled = !isFinal;
            }
        }

        public string LabelText
        {
            set { this.labelPick.Text = value; }
            get { return this.labelPick.Text; }
        }

        private string _date;
        public string Date
        {
            get { return _date; }
            set 
            {
                dateTimePick.Value = DateTime.Now;
                _date = string.Empty;
                if (string.IsNullOrEmpty(value) == false)
                {
                    try
                    {
                        DateTime dt = Convert.ToDateTime(value);
                        _date = dt.ToString(dtFormat);
                        dateTimePick.Value = dt;

                    }
                    catch { }
                }

                SetFormat();
            }
        }

        private void SetFormat()
        {
            if (string.IsNullOrEmpty(_date) == false)
                dateTimePick.CustomFormat = dtFormat;
            else
                dateTimePick.CustomFormat = " ";

            dateTimePick.Format = DateTimePickerFormat.Custom;
        }

        public UcDatePicker()
        {
            InitializeComponent();
            _date = string.Empty;
            SetFormat();
        }

        private void dateTimePick_ValueChanged(object sender, EventArgs e)
        {
            _date = dateTimePick.Value.ToString(dtFormat);
            SetFormat();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            _date = string.Empty;
            SetFormat();
        }

        private void dateTimePick_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                _date = string.Empty;
                SetFormat();
            }
            if (e.Modifiers == Keys.Control)
            {
                if (e.KeyCode == Keys.V)
                {
                    DateTime date;

                    DateTime.TryParse(Clipboard.GetText(), out date);
                    if (date != null && dateTimePick.Enabled)
                    {
                        Date = date.ToString(dtFormat);
                    }
                }
                else
                {
                    if (e.KeyCode == Keys.C)
                    {
                        Clipboard.SetText(Date, TextDataFormat.UnicodeText);
                        e.SuppressKeyPress = true;
                    }
                }
            }
        }

    }
}
